﻿using UnityEngine;
using System.Collections;
using UnityEditor.SceneManagement;
using System;

public class OneKeyDevideMesh
{
    static int count = 0;
    static int maxCount = 65000;
    [UnityEditor.MenuItem("Slg/一键分配Mesh")]
    static void CountMeshNum()
    {
        // PrefabLightmapData[] prefabs = FindObjectsOfType<PrefabLightmapData>();
        GameObject[] prefabs = EditorSceneManager.GetActiveScene().GetRootGameObjects();

        foreach (var instance in prefabs)
        {
            var gameObject = instance.gameObject;

            MeshFilter[] mfArr = gameObject.GetComponentsInChildren<MeshFilter>();
            int curCount = 0;
            int index = 0;
            Transform meshRoot = NewTransform(instance.transform, index);
            foreach (var item in mfArr)
            {
                if (item.sharedMesh == null)
                {
                    continue;
                }
                if (curCount + item.sharedMesh.vertexCount > maxCount)
                {
                    index++;
                    meshRoot = NewTransform(instance.transform, index);
                    curCount = 0;
                }
                item.transform.SetParent(meshRoot);
                curCount += item.sharedMesh.vertexCount;
            }
            // Transform temp = gameObject.transform.GetChild(0);
            // temp = temp.GetChild(0);
            Recursion(gameObject.transform);
        }
        // EditorUtility.ClearProgressBar();
    }

    static Transform GetChild(Transform parent, string name)
    {
        foreach (Transform item in parent)
        {
            if (item.name.Contains(name))
            {
                return item;
            }
        }

        return null;
    }

    static Transform NewTransform(Transform parent, int index)
    {
        string name = string.Format("mesh_{0}", index);
        Transform ret = GetChild(parent, name);
        if (ret == null)
        {
            GameObject meshRoot = new GameObject(name);
            meshRoot.transform.SetParent(parent);
            meshRoot.transform.localPosition = Vector3.zero;
            meshRoot.transform.localScale = Vector3.one;
            ret = meshRoot.transform;
        }

        return ret;
    }

    static void Recursion(Transform root)
    {
        ReName(root.gameObject);
        foreach (Transform item in root)
        {
            Recursion(item);
        }
    }

    static void ReName(GameObject obj)
    {
        var renderers = obj.GetComponentsInChildren<MeshFilter>();
        var verCount = 0;
        for (int i = 0; i < renderers.Length; i++)
        {
            var itemMF = renderers[i];
            if (itemMF.sharedMesh != null)
            {
                verCount += itemMF.sharedMesh.vertexCount;
            }
        }
        if (verCount != 0)
        {
            string[] strs = obj.name.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
            if (strs.Length > 1) obj.name = strs[0];
            // obj.name = string.Format("{0}||{1}",obj.name,verCount);

        }
    }
}
